﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DevExpress.Web.ASPxDataView;
using DevExpress.Web.ASPxEditors;

public partial class Products_Category : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["id"] == null && Request.QueryString["action"] == null)
            Response.Redirect(Request.UrlReferrer.ToString());
        switch (Request.QueryString["action"])
        {
            default:
                lblCation.Text = "Thể loại sách";
                ASPxDataView1.DataSourceID = "SqlDataSourceProductCate";
                break;
            case "newproducts":
                lblCation.Text = "Sản phẩm mới";
                ASPxDataView1.DataSourceID = "SqlDataSourceProductsNew";
                break;
            case "saleoffproducts":
                lblCation.Text = "Sản phẩm giảm giá";
                ASPxDataView1.DataSourceID = "SqlDataSourceProductsSaleOff";
                break;
        }
        ASPxDataView1.DataBind();
    }
    protected void _lnkAddToCart_Click(object sender, EventArgs e)
    {
        if (Session["ShoppingCart"] != null)
        {
            MyShoppingCart sc = Session["ShoppingCart"] as MyShoppingCart;
            if (sc != null)
            {
                Button button = sender as Button;
                if (button == null)
                    throw new Exception("Casting fail");
                bool b = sc.AddItem(int.Parse(button.CommandArgument), 1);
                if (!b)
                    Title = "Add item failure";
                else
                {
                    Title = "Add item success";
                    (Master as IShoppingCart).UpdateShoppingCart();
                }
            }
            else
            {
                Title = "Shopping Cart Session does not be casted";
            }
        }
        else
        {
            Title = "Shopping Cart Sesstion does not found";
        }
    }
    protected void Timer1_Tick(object sender, EventArgs e)
    {
        Title = DateTime.Now.ToString();
        foreach (DataViewItem item in ASPxDataView1.Items)
        {
            ASPxLabel lbl = ASPxDataView1.FindItemControl("ASPxLabel1", item) as ASPxLabel;
            Button btn = ASPxDataView1.FindItemControl("_btnShoppingCart", item) as Button;
            HiddenField hField = ASPxDataView1.FindItemControl("HiddenField1", item) as HiddenField;
            if (lbl != null && btn != null && hField != null)
            {
                DataTable dt =
                    DataAccess.ExecuteQuery(String.Format("SELECT Amount FROM Products WHERE ProductID='{0}'",
                                                          hField.Value));
                if (dt.Rows.Count < 1)
                    continue;
                lbl.Visible = !(btn.Visible = int.Parse(dt.Rows[0]["Amount"].ToString()) <= 0 ? false : true);
            }
        }
    }
}
